package com.example.lettcode._202410._20241017;

import org.springframework.util.CollectionUtils;

import java.util.*;

/*
224. 基本计算器

给你一个字符串表达式 s ，请你实现一个基本计算器来计算并返回它的值。
注意:不允许使用任何将字符串作为数学表达式计算的内置函数，比如 eval() 。



示例 1：

输入：s = "1 + 1"
输出：2
示例 2：

输入：s = " 2-1 + 2 "
输出：3
示例 3：

输入：s = "(1+(4+5+2)-3)+(6+8)"
输出：23


提示：
1 <= s.length <= 3 * 105
s 由数字、'+'、'-'、'('、')'、和 ' ' 组成
s 表示一个有效的表达式
'+' 不能用作一元运算(例如， "+1" 和 "+(2 + 3)" 无效)
'-' 可以用作一元运算(即 "-1" 和 "-(2 + 3)" 是有效的)
输入中不存在两个连续的操作符
每个数字和运行的计算将适合于一个有符号的 32位 整数
1+1
1+（1+1）
11+1+1
11—（-1）

 */
public class _3_Ji_ben_ji_suan_qi {
    public static void main(String[] args) {
        String s = "1-(-(-1))";
        System.out.println(calculate6(s));
    }

    public static int calculate6(String s) {
        Deque<Integer> deque = new LinkedList<>();
        deque.push(1);
        int sign = 1;
        int ret = 0;
        int n = s.length();
        int i = 0;
        while (i < n) {
            if (s.charAt(i) == ' ') {
                i++;
            } else if (s.charAt(i) == '+') {
                sign = deque.peek();
                i++;
            } else if (s.charAt(i) == '-') {
                sign = -deque.peek();
                i++;
            } else if (s.charAt(i) == '(') {
                deque.push(sign);
                i++;
            } else if (s.charAt(i) == ')') {
                deque.pop();
                i++;
            } else {
                long num = 0;
                while (i < n && Character.isDigit(s.charAt(i))) {
                    num = num * 10 + s.charAt(i) - '0';
                    i++;
                }
                ret += sign * num;
            }
        }
        return ret;
    }



    public static int calculate5(String s) {
        Deque<Integer> ops = new LinkedList<>();
        ops.push(1);
        int sign = 1;
        int ret = 0;
        int n = s.length();
        int i = 0;

        while (i < n) {
            if (s.charAt(i) == ' ') {
                i++;
            } else if (s.charAt(i) == '+') {
                sign = ops.peek();
                i++;
            } else if (s.charAt(i) == '-') {
                sign = -ops.peek();
                i++;
            } else if (s.charAt(i) == '(') {
                ops.push(sign);
                i++;
            } else if (s.charAt(i) == ')') {
                ops.pop();
                i++;
            } else {
                long num = 0;
                while (i < n && Character.isDigit(s.charAt(i))) {
                    num += num * 10 + s.charAt(i) - '0';
                    i++;
                }
                ret += sign * num;
            }
        }
        return ret;
    }

    public static int calculate4(String s) {
        Deque<Integer> ops = new LinkedList<Integer>();
        ops.push(1);
        int sign = 1;

        int ret = 0;
        int n = s.length();
        int i = 0;
        while (i < n) {
            if (s.charAt(i) == ' ') {
                i++;
            } else if (s.charAt(i) == '+') {
                sign = ops.peek();
                i++;
            } else if (s.charAt(i) == '-') {
                sign = -ops.peek();
                i++;
            } else if (s.charAt(i) == '(') {
                ops.push(sign);
                i++;
            } else if (s.charAt(i) == ')') {
                ops.pop();
                i++;
            } else {
                long num = 0;
                while (i < n && Character.isDigit(s.charAt(i))) {
                    num = num * 10 + s.charAt(i) - '0';
                    i++;
                }
                ret += sign * num;
            }
        }
        return ret;
    }

    public static int calculate3(String s) {
        Deque<Integer> ops = new LinkedList<Integer>();
        ops.push(1);
        int sign = 1;

        int ret = 0;
        int n = s.length();
        int i = 0;
        while (i < n) {
            if (s.charAt(i) == ' ') {
                i++;
            } else if (s.charAt(i) == '+') {
                sign = ops.peek();
                i++;
            } else if (s.charAt(i) == '-') {
                sign = -ops.peek();
                i++;
            } else if (s.charAt(i) == '(') {
                ops.push(sign);
                i++;
            } else if (s.charAt(i) == ')') {
                ops.pop();
                i++;
            } else {
                long num = 0;
                while (i < n && Character.isDigit(s.charAt(i))) {
                    num = num * 10 + s.charAt(i) - '0';
                    i++;
                }
                ret += sign * num;
            }
        }
        return ret;
    }

    public static int calculate2(String s) {
        s = s.replaceAll("\\s+", "");
        if (s == null || s.length() == 0) {
            return 0;
        }

        Deque<String> linkedList = new LinkedList<>();
        List<String> list = new ArrayList<>();
        String current = "";
        for (int i = 0; i < s.length(); i++) {
            String c = String.valueOf(s.charAt(i));

            if (c == null || c.length() == 0 || "(".equals(c) || ")".equals(c)) {
                continue;
            }
            if ("+".equals(c) || "-".equals(c)) {
                list.add(current);
                list.add(c);
                current = "";
                continue;
            }
            current += c;
        }
        list.add(current);

        String fuhao = "";
        for(String str : list) {
            if ("+".equals(str)) {
                fuhao = "+";
                continue;
            }
            if ("-".equals(str)) {
                fuhao = "-";
                continue;
            }

            if (fuhao != null && "+".equals(fuhao)) {
                linkedList.addLast(str);
                if (linkedList.size() != 1) {
                    String s1 = linkedList.removeLast();
                    String s2 = linkedList.removeLast();
                    Integer ints = Integer.valueOf(s1) + Integer.valueOf(s2);
                    linkedList.push(String.valueOf(ints));
                }
            }else if (fuhao != null && "-".equals(fuhao)) {
                linkedList.addLast(str);
                if (linkedList.size() != 1) {
                    String s1 = linkedList.removeLast();
                    String s2 = linkedList.removeLast();
                    Integer ints = Integer.valueOf(s2) - Integer.valueOf(s1);
                    linkedList.push(String.valueOf(ints));
                }
            } else {

                linkedList.addLast(str);
            }

            fuhao = null;
        }
        return Integer.parseInt(linkedList.pop());
    }

    public static int calculate(String s) {
        s = s.replaceAll("\\s+", "");
        if (s == null || s.length() == 0) {
            return 0;
        }

        Deque<String> linkedList = new LinkedList<>();
        String fuhao = null;
        int sum = 0;
        for (int i = 0; i < s.length(); i++) {
            String c = String.valueOf(s.charAt(i));
            if (c == null || c.length() == 0 || "(".equals(c) || ")".equals(c)) {
                continue;
            }
            if ("+".equals(c)) {
                fuhao = "+";
                continue;
            }
            if ("-".equals(c)) {
                fuhao = "-";
                continue;
            }
            if (fuhao != null && "+".equals(fuhao)) {
                linkedList.addLast(c);
                if (linkedList.size() != 1) {
                    String s1 = linkedList.removeLast();
                    String s2 = linkedList.removeLast();
                    Integer ints = Integer.valueOf(s1) + Integer.valueOf(s2);
                    linkedList.push(String.valueOf(ints));
                }
            }else if (fuhao != null && "-".equals(fuhao)) {
                linkedList.addLast(c);
                if (linkedList.size() != 1) {
                    String s1 = linkedList.removeLast();
                    String s2 = linkedList.removeLast();
                    Integer ints = Integer.valueOf(s2) - Integer.valueOf(s1);
                    linkedList.push(String.valueOf(ints));
                }
            } else {
                linkedList.addLast(c);
            }

            fuhao = null;
        }
        return Integer.parseInt(linkedList.pop());
    }
}
